/* eslint-disable no-undef */
//index.js
import {
    userEdit
} from "../../utils/api/user";
import {
    wxShowToast
} from "../../utils/promisify";
import {
    schoollist,
} from "../../utils/api/course";
//获取应用实例
const app = getApp();

Page({
    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        ImgUrl: app.globalData.ImgUrl,
        displayArea: app.globalData.displayArea,
        schoolPage: 1,
        avatar: null,
        nickname: null,
        gender: null,
        grade: null,
        gradeList: app.globalData.gradeList,
        modal: null,
        schoolName: null,
        schoolId: null,
        nicknameEdit: "",
        universityList: [],
        hasMore: true
    },
    onLoad: function () {
        this.getSchool(this.data.schoolPage);
        this.setData({
            avatar: app.globalData.userInfo.avatar,
            nickname: app.globalData.userInfo.nickname,
            gender: app.globalData.userInfo.gender,
            grade: app.globalData.userInfo.grade,
            schoolName: app.globalData.userInfo.school_name,
            schoolId: app.globalData.userInfo.school_id,
        });
    },
    /**
     * 后退一页
     */
    BackPage() {
        wx.navigateBack({
            delta: 1,
        });
    },
    /**
     * 注销用户
     */
    // logOut() {
    //     wx.removeStorage({
    //         key: "token",
    //         success(res) {
    //             console.log(res);
    //         },
    //     });
    //     app.globalData.userInfo = null;
    // },
    /**
     * 更换头像
     */
    //   replacePicture() {
    //     wx.chooseImage({
    //       count: 1,
    //       sizeType: ["original", "compressed"],
    //       sourceType: ["album", "camera"],
    //       success: (res) => {
    //         // tempFilePath可以作为img标签的src属性显示图片
    //         const tempFilePaths = res.tempFilePaths;
    //         this.setData({
    //           avatar: tempFilePaths[0],
    //         });
    //       },
    //     });
    //   },
    /**
     * 显示弹窗
     */
    showModal(e) {
        let {
            modal
        } = e.currentTarget.dataset;
        if (modal === "university") {
            this.setData({
                sectionModal: true,
            });
            return;
        }
        if (modal === "nickname") {
            this.setData({
                nicknameEdit: this.data.nickname,
            });
        }
        this.setData({
            modal,
        });
    },
    /**
     * 获取学校
     */
    getSchool(page) {
        if (!this.data.hasMore) {
            return;
        }
        schoollist({
            sort: 0,
            page
        }).then(v => {
            this.setData({
                universityList: [...this.data.universityList, ...v.data.data]
            });
            if (v.data.last_page == page) {
                this.setData({
                    hasMore: false,
                });
            } else {
                this.setData({
                    schoolPage: page + 1,
                });
            }
        });
    },
    /**
     * 选择学校
     */
    chooseSchool(e) {
        let {
            id,
            name
        } = e.currentTarget.dataset;
        this.setData({
            schoolName: name,
            schoolId: id,
        });
        this.modifyUserInfo();
    },
    /**
     * 关闭学校弹窗
     */
    hideModal() {
        this.setData({
            sectionModal: false
        });
    },
    scrollBottom() {
        this.getSchool(this.data.schoolPage);
    },
    /**
     * 关闭弹窗
     */
    cancelModal() {
        this.setData({
            modal: false,
            nicknameEdit: "",
        });
    },
    /**
     * 昵称修改
     */
    modifyNickname(e) {
        let value = e.detail.value;
        console.log(value);
        this.setData({
            nicknameEdit: value,
        });
    },
    /**
     * 修改用户信息
     */
    modifyUserInfo() {
        let {
            avatar,
            nickname,
            gender,
            grade,
            schoolId
        } = this.data;
        userEdit({
            avatar,
            nickname,
            gender,
            grade,
            school_id: schoolId
        }).then((v) => {
            console.log(v);
            v.code && wxShowToast(v.msg);
            app.getSet();
        });
    },
    /**
     * 昵称修改确认
     */
    nicknameConfirm() {
        let {
            nicknameEdit
        } = this.data;
        this.setData({
            nickname: nicknameEdit,
        });
        this.modifyUserInfo();
        this.cancelModal();
    },
    /**
     * 性别更改
     */
    modifyGender(e) {
        let gender = e.currentTarget.dataset.id;
        this.setData({
            gender,
        });
        this.modifyUserInfo();
        this.cancelModal();
    },
    /**
     * 年级更改
     */
    modifyGrade(e) {
        let grade = e.currentTarget.dataset.id;
        this.setData({
            grade,
        });
        this.modifyUserInfo();
        this.cancelModal();
    },
    /**
     * 同步头像昵称
     */
    syncUserInfo() {
        wx.getUserProfile({
            desc: "用于完善用户资料", // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (res) => {
                console.log("res.userInfo", res.userInfo);
                let {
                    nickName,
                    avatarUrl
                } = res.userInfo;
                this.setData({
                    nickname: nickName,
                    avatar: avatarUrl
                });
                this.modifyUserInfo();
            }
        });
    },
});